using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;

namespace CMS.Web
{
    public class Files : BaseUserControl
    {
        public static string UploadPath = System.Configuration.ConfigurationSettings.AppSettings["uploadPath"];

        #region Create Instance
        private static Files instance = null;
        public static Files Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Files();
                }
                return instance;
            }
        }
        #endregion

        public string UpLoadFile(HttpPostedFile pFile, string strFolderPath)
        {
            string strSavePath = strFolderPath; //UploadPath + strFolderPath;//
            string fullPath = UploadPath;
            if (!strSavePath.Equals(""))
            {
                if (!Directory.Exists(UploadPath + strSavePath))
                    Directory.CreateDirectory(UploadPath + strSavePath);

                fullPath = Path.Combine(fullPath, strSavePath);
            }

            if (pFile != null)
            {
                int iFileLen = pFile.ContentLength;
                if (iFileLen == 0)
                {
                    return string.Empty;
                }

                string strFilename = this.StandardName(fullPath, pFile);
                try
                {
                    fullPath = Path.Combine(fullPath, strFilename);
                    pFile.SaveAs(fullPath);

                    return strFilename;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        public string StandardName(string strFolderPath, HttpPostedFile pFile)
        {
            string path = Path.Combine(UploadPath, strFolderPath);
            string strFilename = System.IO.Path.GetFileName(pFile.FileName);
            string strEx = System.IO.Path.GetExtension(pFile.FileName);
            int iEx = 0;
            try
            {
                while (System.IO.File.Exists(Path.Combine(path, strFilename)))
                {
                    iEx++;
                    strFilename = System.IO.Path.GetFileNameWithoutExtension(pFile.FileName) + "_" + iEx.ToString() + strEx;
                }
            }
            catch (Exception ex)
            {
            }
            return strFilename;
        }

        public bool DeleteFile(bool bStatus, string strFolderPath, string fileName)
        {
            if (bStatus)
            {
                string path = Path.Combine(UploadPath, strFolderPath);
                //if (System.IO.File.Exists(Server.MapPath(strSavePath + strPathFile)))
                if (System.IO.File.Exists(Path.Combine(path, fileName)))
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo(Path.Combine(path, fileName));
                    fi.Delete();
                }
            }
            return bStatus;
        }

        // Load the file into HttpPostedFile obj
        public byte[] LoadFileToOject(string fileName)
        {
            byte[] res = null;
            string filePath = Path.Combine(UploadPath, fileName);
            // Check exist file
            if (System.IO.File.Exists(filePath))
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, (int)fs.Length);
                    res = buffer;
                }
            }
            return res;
        }

        /**
         * Save attach file
         */
        public bool SaveAttachFile(string fileName, byte[] bFile)
        {
            bool res = true;
            //string year = FileUltils.Instance.createFolder(DateTime.Today.Year.ToString());
            string strSavePath = UploadPath; //Path.Combine(UploadPath, year); ;
            if (bFile.Length > 0)
            {
                string strFilename = this.StandardName(strSavePath, fileName);
                try
                {
                    strSavePath = Path.Combine(strSavePath, strFilename);
                    FileStream fs = new FileStream(strSavePath, FileMode.Create, FileAccess.Write);
                    fs.Write(bFile, 0, bFile.Length);
                    fs.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }

                return res;
            }
            else
            {
                return false;
            }

            return res;
        }

        public string StandardName(string strFolderPath, string fileName)
        {
            string path = Path.Combine(UploadPath, strFolderPath);
            string strFilename = System.IO.Path.GetFileName(fileName);
            string strEx = System.IO.Path.GetExtension(fileName);
            int iEx = 0;
            try
            {
                while (System.IO.File.Exists(Path.Combine(path, strFilename)))
                {
                    iEx++;
                    strFilename = System.IO.Path.GetFileNameWithoutExtension(fileName) + "_" + iEx.ToString() + strEx;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }
            return strFilename;
        }
    }
}
